package com.fourforfo.fourmall.order.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;
import com.fourforfo.common.utils.R;
import com.fourforfo.fourmall.order.entity.OrderEntity;
import com.fourforfo.fourmall.order.service.OrderService;
import com.fourforfo.fourmall.order.vo.OrderConfirmVO;
import com.fourforfo.fourmall.order.vo.OrderSubmitResponseVO;
import com.fourforfo.fourmall.order.vo.OrderSubmitVO;
import com.fourforfo.fourmall.order.vo.alipay.PayVO;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: xxl-sso-core
 * @description:
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-08-31 16:15
 **/
@Slf4j
@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;


    @GetMapping("/memberOrder.html")
    public String memberOrderPage(@RequestParam(value="pageNum",defaultValue = "1")Integer pageNum, HttpServletRequest request, Model model){

        //查出当前登录的用户的所有订单列表数据
        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);

        //封装页数参数
        Map<String,Object> params = new HashMap<>();
        params.put("page",pageNum.toString());
        PageUtils page = orderService.listWithItem(params,xxlUser.getUserid());
        model.addAttribute("order_page",page);

        return "order_list";
    }

    @GetMapping("/deleteOrder")
    public String deleteOrder(@RequestParam(value="orderSn")String orderSn){
        boolean order_sn = orderService.remove(new QueryWrapper<OrderEntity>().eq("order_sn", orderSn));
        return "redirect:http://order.fourmall.com/memberOrder.html";
    }


    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpServletRequest request){


        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);

        if(xxlUser==null || StringUtils.isEmpty(xxlUser.getUserid())){
            model.addAttribute("error","未获取到登录用户信息，不能访问该页面");
            return "error";
        }
        OrderConfirmVO confirmVO = orderService.confirmOrder(xxlUser);

        model.addAttribute("orderConfirmData",confirmVO);

        return "confirm";
    }


    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO submitVO, HttpServletRequest request, Model model, RedirectAttributes attributes){
        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);

        //1.创建订单、验证token令牌、验证价格、锁库存
        OrderSubmitResponseVO responseVO = orderService.submitOrder(submitVO,xxlUser);
        String msg="下单失败：";
        if(responseVO.getCode()==0){
            //下单成功后来到支付选择页
            model.addAttribute("submitOrderResp",responseVO);
            return "pay";
        }else if(responseVO.getCode()==1){
            log.warn("重复提交导致幂等性验证失败！");
            msg += "重复提交导致幂等性验证失败！";
        }else if(responseVO.getCode()==2){
            log.warn("金额对比失败,用户生成订单时有其他操作导致其他购物项生成！");
            msg += "金额对比失败,用户生成订单时有其他操作导致其他购物项生成！";
        }else if(responseVO.getCode()==3){
            log.warn("库存锁定失败!");
            msg += "库存锁定失败!";
        }else{
            log.warn("创建订单时出现未知错误！");
            msg += "创建订单时出现未知错误！";
        }
        attributes.addFlashAttribute("msg",msg);
        //下单失败回到订单确认页重新确认订单信息
        return "redirect:http://order.fourmall.com/toTrade";
    }

}
